package han.chensing.latos.maker.windows.widget;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.utils.Align;

import java.util.ArrayList;
import java.util.List;

import han.chensing.latos.maker.GameMain;
import han.chensing.latos.maker.res.Res;
import space.earlygrey.shapedrawer.ShapeDrawer;

public class ShortCutWidget extends Widget {
    List<KeyConfig> keyConfigs;

    public static final int NUM_SHIFT = 0;
    public static final int NUM_CONTROL = 1;
    public static final int NUM_ALT = 2;

    public ShortCutWidget(){
        keyConfigs=new ArrayList<>();
        register(NUM_SHIFT,"S",false,Input.Keys.SHIFT_RIGHT,Input.Keys.SHIFT_LEFT);
        register(NUM_CONTROL,"C",false,Input.Keys.CONTROL_RIGHT,Input.Keys.CONTROL_LEFT);
        register(NUM_ALT,"A",false,Input.Keys.ALT_RIGHT,Input.Keys.ALT_LEFT);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        float x = getX();
        float y = getY();
        float width = getWidth();
        float height = getHeight();
        ShapeDrawer shapeRenderer = GameMain.shapeDrawer;
        shapeRenderer.rectangle(x,y,width,height,
                Color.WHITE);
        updateStuckKeys();
        drawIndividualLine();
        drawKeyStatus(batch);
    }

    private void updateStuckKeys(){
        for (KeyConfig keyConfig : keyConfigs) {
            if(!keyConfig.stuck)continue;
            if (isKeyJustPressed(keyConfig))keyConfig.hit=!keyConfig.hit;
        }
    }

    private void drawIndividualLine(){
        float x = getX();
        float y = getY();
        float width = getWidth();
        float height = getHeight();
        float v = width / (float) keyConfigs.size();
        ShapeDrawer shapeRenderer = GameMain.shapeDrawer;
        for(int i=1;i<keyConfigs.size();i++) {
            float v1 = x + v * i;
            shapeRenderer.line(v1,y,v1,y+height,1);
        }
    }

    private void drawKeyStatus(Batch batch){
        float x = getX();
        float y = getY();
        float width = getWidth();
        float height = getHeight();
        BitmapFont fontConsolas = Res.get().get("fontDeng", BitmapFont.class);
        float v = width / keyConfigs.size();
        for (int i = 0; i < keyConfigs.size(); i++) {
            KeyConfig keyConfig = keyConfigs.get(i);
            boolean keyPressed = isKeyPressed(keyConfig);
            if (!keyPressed) fontConsolas.setColor(1,1,1,.3f);
            fontConsolas.draw(
                    batch,keyConfig.hint,
                    x+v*i,y+6f+height/2f,v, Align.center,false
            );
            if (!keyPressed)fontConsolas.setColor(1,1,1,1);
        }
    }

    private boolean isKeyPressed(KeyConfig config){
        if (config.stuck)return config.hit;
        int[] keys = config.keys;
        for (int key : keys)
            if (Gdx.input.isKeyPressed(key))return true;
        return false;
    }

    private boolean isKeyJustPressed(KeyConfig config){
        int[] keys = config.keys;
        boolean result=false;
        for (int key : keys)
            if (Gdx.input.isKeyJustPressed(key))result=true;
        return result;
    }


    private KeyConfig searchConfigByNum(int num){
        for (KeyConfig keyConfig : keyConfigs)
            if (keyConfig.num==num)return keyConfig;
        return null;
    }

    public void register(int num,String hint,boolean stuck,int... keys){
        keyConfigs.add(new KeyConfig(num,hint,stuck,keys));
    }

    public boolean isKeyPressed(int num){
        KeyConfig keyConfig = searchConfigByNum(num);
        if (keyConfig==null)return false;
        if (keyConfig.stuck)return keyConfig.hit;
        return isKeyPressed(keyConfig);
    }

    public void setKeyPressed(int num,boolean to){
        KeyConfig keyConfig = searchConfigByNum(num);
        if (keyConfig==null)return;
        keyConfig.hit=to;
    }

    public void reverseKeyPressed(int num){
        KeyConfig keyConfig = searchConfigByNum(num);
        if (keyConfig==null)return;
        keyConfig.hit=!keyConfig.hit;
    }

    private static class KeyConfig {
        int num;
        int[] keys;
        boolean stuck;
        String hint;
        boolean hit=false;

        public KeyConfig(int num,String hint,boolean stuck,int... keys) {
            this.keys = keys;
            this.hint = hint;
            this.num=num;
            this.stuck=stuck;
        }
    }
}
